import sys
from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow, QPushButton, QLabel
from PyQt5.QtCore import QRect
from PyQt5.QtGui import QPixmap

import Maze.DFS_maze as dfs
import Maze.prim_maze as prim
import Maze.RD_maze as rd
import Maze.OG_map as og

import PathPlanning.DFS as dfs_path

import numpy as np
'''
按键:迷宫生成按键:DFS,PRIM,RD
     解迷宫按键:DFS

图片：迷宫，迷宫解法
'''

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.resize(540, 650)
        self.setWindowTitle("迷宫")

        # 生成迷宫按键
        self.label1 = QLabel(self)
        self.label1.setText("生成迷宫")
        self.label1.move(10, 550)

        self.button1 = QPushButton("DFS", self)
        self.button1.move(85, 550)        
        self.button1.clicked.connect(self.clickButton1)

        self.button2 = QPushButton("PRIM", self)
        self.button2.move(200, 550)
        self.button2.clicked.connect(self.clickButton2)

        self.button3 = QPushButton("RD", self)
        self.button3.move(315, 550)
        self.button3.clicked.connect(self.clickButton3)

        # 解迷宫按键
        self.label2 = QLabel(self)
        self.label2.setText("解迷宫")
        self.label2.move(10, 600)

        self.button4 = QPushButton("DFS", self)
        self.button4.move(85, 600)
        self.button4.clicked.connect(self.clickButton4)

        self.image = QLabel(self)
        self.image.setGeometry(QRect(20, 20, 500, 500))
        self.image.setText("")
        self.image.setObjectName("迷宫图")

        self.Map = np.zeros((50*10,50*10), dtype=np.uint8)
        self.OG_Map = np.zeros((len(self.Map), len(self.Map)), dtype=np.uint8)
        
    def set_image(self, filepath):    
        self.image.setPixmap(QPixmap(filepath).scaled(500, 500))
        self.image.setScaledContents(True)

    def clickButton1(self):
        CreateMaze = dfs.DFS_Maze(30, 30)
        self.Map = CreateMaze.run()
        self.OG_Map = og.Occupancy_Grid(self.Map)
        self.set_image("./UI/maze.jpg")

    def clickButton2(self):
        CreateMaze = prim.Prim_Maze(30, 30)
        self.Map = CreateMaze.run()
        self.OG_Map = og.Occupancy_Grid(self.Map)
        self.set_image("./UI/maze.jpg")

    def clickButton3(self):
        CreateMaze = rd.RD_Maze(30, 30)
        self.Map = CreateMaze.run()
        self.OG_Map = og.Occupancy_Grid(self.Map)
        self.set_image("./UI/maze.jpg")

    def clickButton4(self):
        dfs_path.plan(self.OG_Map, self.Map)
        self.set_image("./UI/result.jpg")

def display():
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())
